-- Encoding function tests for PostgreSQL.

-- If the encoding.csv file located at
-- https://github.com/google/open-location-code/blob/main/test_data/encoding.csv
-- is updated, run the update_encoding_tests.sh script.

-- RAISE is not supported directly in SELECT statements, it must be called from a function.
CREATE FUNCTION raise_error(msg text) RETURNS integer
LANGUAGE plpgsql AS
$$BEGIN
RAISE EXCEPTION '%', msg;
RETURN 42;
END;$$;

CREATE TABLE encoding_tests (
    latitude_degrees NUMERIC NOT NULL,
    longitude_degrees NUMERIC NOT NULL,
    latitude_integer BIGINT NOT NULL,
    longitude_integer BIGINT NOT NULL,
    code_length INTEGER NOT NULL,
    code TEXT NOT NULL
);
INSERT INTO encoding_tests VALUES (20.375, 2.775, 2759375000, 1497292800, 6, '7FG49Q00+');
INSERT INTO encoding_tests VALUES (20.3700625, 2.7821875, 2759251562, 1497351680, 10, '7FG49QCJ+2V');
INSERT INTO encoding_tests VALUES (20.3701125, 2.782234375, 2759252812, 1497352064, 11, '7FG49QCJ+2VX');
INSERT INTO encoding_tests VALUES (20.3701135, 2.78223535156, 2759252837, 1497352071, 13, '7FG49QCJ+2VXGJ');
INSERT INTO encoding_tests VALUES (47.0000625, 8.0000625, 3425001562, 1540096512, 10, '8FVC2222+22');
INSERT INTO encoding_tests VALUES (-41.2730625, 174.7859375, 1218173437, 2906406400, 10, '4VCPPQGP+Q9');
INSERT INTO encoding_tests VALUES (0.5, -179.5, 2262500000, 4096000, 4, '62G20000+');
INSERT INTO encoding_tests VALUES (-89.5, -179.5, 12500000, 4096000, 4, '22220000+');
INSERT INTO encoding_tests VALUES (20.5, 2.5, 2762500000, 1495040000, 4, '7FG40000+');
INSERT INTO encoding_tests VALUES (-89.9999375, -179.9999375, 1562, 512, 10, '22222222+22');
INSERT INTO encoding_tests VALUES (0.5, 179.5, 2262500000, 2945024000, 4, '6VGX0000+');
INSERT INTO encoding_tests VALUES (1, 1, 2275000000, 1482752000, 11, '6FH32222+222');
INSERT INTO encoding_tests VALUES (90, 1, 4499999999, 1482752000, 4, 'CFX30000+');
INSERT INTO encoding_tests VALUES (92, 1, 4499999999, 1482752000, 4, 'CFX30000+');
INSERT INTO encoding_tests VALUES (90, 1, 4499999999, 1482752000, 10, 'CFX3X2X2+X2');
INSERT INTO encoding_tests VALUES (1, 180, 2275000000, 0, 4, '62H20000+');
INSERT INTO encoding_tests VALUES (1, 181, 2275000000, 8192000, 4, '62H30000+');
INSERT INTO encoding_tests VALUES (20.3701135, 362.78223535156, 2759252837, 1497352071, 13, '7FG49QCJ+2VXGJ');
INSERT INTO encoding_tests VALUES (47.0000625, 728.0000625, 3425001562, 1540096512, 10, '8FVC2222+22');
INSERT INTO encoding_tests VALUES (-41.2730625, 1254.7859375, 1218173437, 2906406400, 10, '4VCPPQGP+Q9');
INSERT INTO encoding_tests VALUES (20.3701135, -357.217764648, 2759252837, 1497352072, 13, '7FG49QCJ+2VXGJ');
INSERT INTO encoding_tests VALUES (47.0000625, -711.9999375, 3425001562, 1540096512, 10, '8FVC2222+22');
INSERT INTO encoding_tests VALUES (-41.2730625, -905.2140625, 1218173437, 2906406400, 10, '4VCPPQGP+Q9');
INSERT INTO encoding_tests VALUES (1.2, 3.4, 2280000000, 1502412800, 10, '6FH56C22+22');
INSERT INTO encoding_tests VALUES (37.539669125, -122.375069724, 3188491728, 472063428, 15, '849VGJQF+VX7QR3J');
INSERT INTO encoding_tests VALUES (37.539669125, -122.375069724, 3188491728, 472063428, 16, '849VGJQF+VX7QR3J');
INSERT INTO encoding_tests VALUES (37.539669125, -122.375069724, 3188491728, 472063428, 100, '849VGJQF+VX7QR3J');
INSERT INTO encoding_tests VALUES (35.6, 3.033, 3140000000, 1499406336, 10, '8F75J22M+26');
INSERT INTO encoding_tests VALUES (-48.71, 142.78, 1032250000, 2644213760, 8, '4R347QRJ+');
INSERT INTO encoding_tests VALUES (-70, 163.7, 500000000, 2815590400, 8, '3V252P22+');
INSERT INTO encoding_tests VALUES (-2.804, 7.003, 2179900000, 1531928576, 13, '6F9952W3+C6222');
INSERT INTO encoding_tests VALUES (13.9, 164.88, 2597500000, 2825256960, 12, '7V56WV2J+2222');
INSERT INTO encoding_tests VALUES (-13.23, 172.77, 1919250000, 2889891840, 8, '5VRJQQCC+');
INSERT INTO encoding_tests VALUES (40.6, 129.7, 3265000000, 2537062400, 8, '8QGFJP22+');
INSERT INTO encoding_tests VALUES (-52.166, 13.694, 945850000, 1586741248, 14, '3FVMRMMV+JJ2222');
INSERT INTO encoding_tests VALUES (-14, 106.9, 1900000000, 2350284800, 6, '5PR82W00+');
INSERT INTO encoding_tests VALUES (70.3, -87.64, 4007500000, 756613120, 13, 'C62J8926+22222');
INSERT INTO encoding_tests VALUES (66.89, -106, 3922250000, 606208000, 10, '95RPV2R2+22');
INSERT INTO encoding_tests VALUES (2.5, -64.23, 2312500000, 948387840, 11, '67JQGQ2C+222');
INSERT INTO encoding_tests VALUES (-56.7, -47.2, 832500000, 1087897600, 14, '38MJ8R22+222222');
INSERT INTO encoding_tests VALUES (-34.45, -93.719, 1388750000, 706813952, 6, '46Q8H700+');
INSERT INTO encoding_tests VALUES (-35.849, -93.75, 1353775000, 706560000, 12, '46P85722+C222');
INSERT INTO encoding_tests VALUES (65.748, 24.316, 3893700000, 1673756672, 12, '9GQ6P8X8+6C22');
INSERT INTO encoding_tests VALUES (-57.32, 130.43, 817000000, 2543042560, 12, '3QJGMCJJ+2222');
INSERT INTO encoding_tests VALUES (17.6, -44.4, 2690000000, 1110835200, 6, '789QJJ00+');
INSERT INTO encoding_tests VALUES (-27.6, -104.8, 1560000000, 616038400, 6, '554QC600+');
INSERT INTO encoding_tests VALUES (41.87, -145.59, 3296750000, 281886720, 13, '83HPVCC6+22222');
INSERT INTO encoding_tests VALUES (-4.542, 148.638, 2136450000, 2692202496, 13, '6R7CFJ5Q+66222');
INSERT INTO encoding_tests VALUES (-37.014, -159.936, 1324650000, 164364288, 10, '43J2X3P7+CJ');
INSERT INTO encoding_tests VALUES (-57.25, 125.49, 818750000, 2502574080, 15, '3QJ7QF2R+2222222');
INSERT INTO encoding_tests VALUES (48.89, -80.52, 3472250000, 814940160, 13, '86WXVFRJ+22222');
INSERT INTO encoding_tests VALUES (53.66, 170.97, 3591500000, 2875146240, 14, '9V5GMX6C+222222');
INSERT INTO encoding_tests VALUES (0.49, -76.97, 2262250000, 844021760, 15, '67G5F2RJ+2222222');
INSERT INTO encoding_tests VALUES (40.44, -36.7, 3261000000, 1173913600, 12, '89G5C8R2+2222');
INSERT INTO encoding_tests VALUES (58.73, 69.95, 3718250000, 2047590400, 8, '9JCFPXJ2+');
INSERT INTO encoding_tests VALUES (16.179, 150.075, 2654475000, 2703974400, 12, '7R8G53HG+J222');
INSERT INTO encoding_tests VALUES (-55.574, -70.061, 860650000, 900620288, 12, '37PFCWGQ+CJ22');
INSERT INTO encoding_tests VALUES (76.1, -82.5, 4152500000, 798720000, 15, 'C68V4G22+2222222');
INSERT INTO encoding_tests VALUES (58.66, 149.17, 3716500000, 2696560640, 10, '9RCFM56C+22');
INSERT INTO encoding_tests VALUES (-67.2, 48.6, 570000000, 1872691200, 6, '3H4CRJ00+');
INSERT INTO encoding_tests VALUES (-5.6, -54.5, 2110000000, 1028096000, 14, '6867CG22+222222');
INSERT INTO encoding_tests VALUES (-34, 145.5, 1400000000, 2666496000, 14, '4RR72G22+222222');
INSERT INTO encoding_tests VALUES (-34.2, 66.4, 1395000000, 2018508800, 12, '4JQ8RC22+2222');
INSERT INTO encoding_tests VALUES (17.8, -108.5, 2695000000, 585728000, 6, '759HRG00+');
INSERT INTO encoding_tests VALUES (10.734, -168.294, 2518350000, 95895552, 10, '722HPPM4+JC');
INSERT INTO encoding_tests VALUES (-28.732, 54.32, 1531700000, 1919549440, 8, '5H3P789C+');
INSERT INTO encoding_tests VALUES (64.1, 107.9, 3852500000, 2358476800, 12, '9PP94W22+2222');
INSERT INTO encoding_tests VALUES (79.7525, 6.9623, 4243812500, 1531595161, 8, 'CFF8QX36+');
INSERT INTO encoding_tests VALUES (-63.6449, -25.1475, 658877500, 1268551680, 8, '398P9V43+');
INSERT INTO encoding_tests VALUES (35.019, 148.827, 3125475000, 2693750784, 11, '8R7C2R9G+JR2');
INSERT INTO encoding_tests VALUES (71.132, -98.584, 4028300000, 666959872, 15, 'C6334CJ8+RC22222');
INSERT INTO encoding_tests VALUES (53.38, -51.34, 3584500000, 1053982720, 12, '985C9MJ6+2222');
INSERT INTO encoding_tests VALUES (-1.2, 170.2, 2220000000, 2868838400, 12, '6VCGR622+2222');
INSERT INTO encoding_tests VALUES (50.2, -162.8, 3505000000, 140902400, 11, '922V6622+222');
INSERT INTO encoding_tests VALUES (-25.798, -59.812, 1605050000, 984580096, 10, '5862652Q+R6');
INSERT INTO encoding_tests VALUES (81.654, -162.422, 4291350000, 143998976, 14, 'C2HVMH3H+J62222');
INSERT INTO encoding_tests VALUES (-75.7, -35.4, 357500000, 1184563200, 8, '29P68J22+');
INSERT INTO encoding_tests VALUES (67.2, 115.1, 3930000000, 2417459200, 11, '9PVQ6422+222');
INSERT INTO encoding_tests VALUES (-78.137, -42.995, 296575000, 1122344960, 12, '28HVV274+6222');
INSERT INTO encoding_tests VALUES (-56.3, 114.5, 842500000, 2412544000, 11, '3PMPPG22+222');
INSERT INTO encoding_tests VALUES (10.767, -62.787, 2519175000, 960208896, 13, '772VQ687+R6222');
INSERT INTO encoding_tests VALUES (-19.212, 107.423, 1769700000, 2354569216, 10, '5PG9QCQF+66');
INSERT INTO encoding_tests VALUES (21.192, -45.145, 2779800000, 1104732160, 15, '78HP5VR4+R222222');
INSERT INTO encoding_tests VALUES (16.701, 148.648, 2667525000, 2692284416, 14, '7R8CPJ2X+C62222');
INSERT INTO encoding_tests VALUES (52.25, -77.45, 3556250000, 840089600, 15, '97447H22+2222222');
INSERT INTO encoding_tests VALUES (-68.54504, -62.81725, 536374000, 959961088, 11, '373VF53M+X4J');
INSERT INTO encoding_tests VALUES (76.7, -86.172, 4167500000, 768638976, 12, 'C68MPR2H+2622');
INSERT INTO encoding_tests VALUES (-6.2, 96.6, 2095000000, 2265907200, 13, '6M5RRJ22+22222');
INSERT INTO encoding_tests VALUES (59.32, -157.21, 3733000000, 186695680, 12, '93F48QCR+2222');
INSERT INTO encoding_tests VALUES (29.7, 39.6, 2992500000, 1798963200, 12, '7GXXPJ22+2222');
INSERT INTO encoding_tests VALUES (-18.32, 96.397, 1792000000, 2264244224, 10, '5MHRM9JW+2R');
INSERT INTO encoding_tests VALUES (-30.3, 76.5, 1492500000, 2101248000, 11, '4JXRPG22+222');
INSERT INTO encoding_tests VALUES (50.342, -112.534, 3508550000, 552681472, 15, '95298FR8+RC22222');
INSERT INTO encoding_tests VALUES (80.0100000001, 58.57, 4250250000, 1954365440, 15, 'CHGW2H6C+2222222');
INSERT INTO encoding_tests VALUES (80.00999996, 58.57, 4250249999, 1954365440, 15, 'CHGW2H5C+X2RRRRR');
INSERT INTO encoding_tests VALUES (-80.0099999999, 58.57, 249750000, 1954365440, 15, '2HFWXHRC+2222222');
INSERT INTO encoding_tests VALUES (-80.0100000399, 58.57, 249749999, 1954365440, 15, '2HFWXHQC+X2RRRRR');
INSERT INTO encoding_tests VALUES (47.000000080000000, 8.00022229, 3425000002, 1540097820, 15, '8FVC2222+235235C');
INSERT INTO encoding_tests VALUES (68.3500147997595, 113.625636875353, 3958750369, 2405381217, 15, '9PWM9J2G+272FWJV');
INSERT INTO encoding_tests VALUES (38.1176000887231, 165.441989844555, 3202940002, 2829860780, 15, '8VC74C9R+2QX445C');
INSERT INTO encoding_tests VALUES (-28.1217794010122, -154.066811473758, 1546955514, 212444680, 15, '5337VWHM+77PR2GR');
INSERT INTO encoding_tests VALUES (37.539669125, -122.375069724, 3188491728, 472063428, 2, '84000000+');
INSERT INTO encoding_tests VALUES (51.1276857, -184.2279861, 3528192142, 2914484337, 11, '9V3Q4QHC+3RC');
INSERT INTO encoding_tests VALUES (-93.84140, -162.06820, 0, 146897305, 10, '222V2W2J+2P');
INSERT INTO encoding_tests VALUES (-25.1585965, -176.4414937, 1621035087, 29151283, 14, '5265RHR5+HC62QC');
INSERT INTO encoding_tests VALUES (82.806550, 30.229187, 4320163750, 1722197499, 13, 'CGJGR64H+JMF55');
INSERT INTO encoding_tests VALUES (52.67256, -4.55204, 3566814000, 1437269688, 13, '9C4QMCFX+25GG5');
INSERT INTO encoding_tests VALUES (14.9420223132, -24.1698775963, 2623550557, 1276560362, 2, '79000000+');
INSERT INTO encoding_tests VALUES (50.46, 112.02, 3511500000, 2392227840, 12, '9P2JF26C+2222');
INSERT INTO encoding_tests VALUES (-72.929463, 42.000964, 426763425, 1818631897, 4, '2HV40000+');
INSERT INTO encoding_tests VALUES (76.091456, -125.608062, 4152286400, 445578756, 8, 'C48P39RR+');
INSERT INTO encoding_tests VALUES (-94.103, -38.308, 0, 1160740864, 14, '29232M2R+2R2222');
INSERT INTO encoding_tests VALUES (88.1, 86.0, 4452500000, 2179072000, 4, 'CMW80000+');
INSERT INTO encoding_tests VALUES (-44.545247, -40.700335, 1136368825, 1141142855, 10, '487XF73X+WV');
INSERT INTO encoding_tests VALUES (20.67, -133.40, 2766750000, 381747200, 8, '74G8MJC2+');
INSERT INTO encoding_tests VALUES (91.37590, -96.45974, 4499999999, 684361809, 10, 'C6X5XGXR+X4');
INSERT INTO encoding_tests VALUES (64.61, -192.97, 3865250000, 2842869760, 12, '9VP9J26J+2222');
INSERT INTO encoding_tests VALUES (-19.427, -156.355, 1764325000, 193699840, 12, '53G5HJFW+6222');
INSERT INTO encoding_tests VALUES (-77.172610657, -122.783537134, 320684733, 468717263, 8, '24JVR6G8+');
INSERT INTO encoding_tests VALUES (-48, -141, 1050000000, 319488000, 10, '434X2222+22');
INSERT INTO encoding_tests VALUES (-48, -111, 1050000000, 565248000, 2, '45000000+');
INSERT INTO encoding_tests VALUES (34.59271625, 33.43832676, 3114817906, 1748486772, 15, '8G6MHCVQ+38PM976');
INSERT INTO encoding_tests VALUES (-18.70036, -9.64681, 1782491000, 1395533332, 6, '5CHG7900+');
INSERT INTO encoding_tests VALUES (82.14, 194.83, 4303500000, 121487360, 6, 'C2JP4R00+');
INSERT INTO encoding_tests VALUES (-83.0611, -53.5201, 173472500, 1036123340, 6, '2888WF00+');
INSERT INTO encoding_tests VALUES (-90.5, -61.8, 0, 968294400, 14, '272W2622+222222');
INSERT INTO encoding_tests VALUES (23.857492947, -38.922971931, 2846437323, 1155703013, 8, '79M3V34G+');
INSERT INTO encoding_tests VALUES (71.301289, -127.202151, 4032532225, 432519979, 15, 'C43J8Q2X+G49CW45');
INSERT INTO encoding_tests VALUES (22.613410, -65.531218, 2815335250, 937728262, 2, '77000000+');
INSERT INTO encoding_tests VALUES (-59.5, 100.8, 762500000, 2300313600, 2, '3P000000+');
INSERT INTO encoding_tests VALUES (87.021195762, -199.388732204, 4425529894, 2790287505, 15, 'CVV22JC6+FGCW3JV');
INSERT INTO encoding_tests VALUES (58.5932701, 172.4650093, 3714831752, 2887393356, 12, '9VCJHFV8+822V');
INSERT INTO encoding_tests VALUES (-31.17610, 41.37565, 1470597500, 1813509324, 8, '4HW3R9FG+');
INSERT INTO encoding_tests VALUES (44, 58, 3350000000, 1949696000, 6, '8HPW2200+');
INSERT INTO encoding_tests VALUES (-4.0070, 154.7493, 2149825000, 2742266265, 6, '6R7PXP00+');
INSERT INTO encoding_tests VALUES (2.8, -119.9, 2320000000, 492339200, 12, '65J2R422+2222');
INSERT INTO encoding_tests VALUES (77.296962202, -118.449652886, 4182424055, 504220443, 4, 'C5930000+');
INSERT INTO encoding_tests VALUES (35.48003, 96.52265, 3137000750, 2265273548, 15, '8M7RFGJF+2369252');
INSERT INTO encoding_tests VALUES (52.42264, 60.49549, 3560566000, 1970139054, 8, '9J42CFFW+');
INSERT INTO encoding_tests VALUES (29.096, 166.130, 2977400000, 2835496960, 10, '7VX834WJ+C2');
INSERT INTO encoding_tests VALUES (67.496291, 38.248585, 3937407275, 1787892408, 10, '9GVWF6WX+GC');
INSERT INTO encoding_tests VALUES (69.298163526, -181.784436557, 3982454088, 2934501895, 11, '9VXW76X8+768');
INSERT INTO encoding_tests VALUES (48.44527393761, 195.13608085747, 3461131848, 123994774, 8, '82WQC4WP+');
INSERT INTO encoding_tests VALUES (-28.8394, 166.9146, 1529015000, 2841924403, 6, '5V385W00+');
INSERT INTO encoding_tests VALUES (46.01263, 109.23175, 3400315750, 2369386496, 15, '8PRF267J+3P26222');
INSERT INTO encoding_tests VALUES (-61.385416741, -100.103564052, 715364581, 654511603, 8, '35CXJV7W+');
INSERT INTO encoding_tests VALUES (85.6301065, 194.7590568, 4390752662, 120906193, 8, 'C2QPJQJ5+');
INSERT INTO encoding_tests VALUES (-74.602, 189.932, 384950000, 81362944, 8, '22QF9WXJ+');
INSERT INTO encoding_tests VALUES (-90.930, -145.371, 0, 283680768, 11, '232P2J2H+2J2');
INSERT INTO encoding_tests VALUES (-58.618133, 64.746630, 784546675, 2004964392, 4, '3JH60000+');
INSERT INTO encoding_tests VALUES (66.1423, -96.6000, 3903557500, 683212800, 10, '96R54CR2+W2');
INSERT INTO encoding_tests VALUES (-39.962, 168.233, 1250950000, 2852724736, 4, '4VGC0000+');
INSERT INTO encoding_tests VALUES (98.31, 86.17, 4499999999, 2180464640, 11, 'CMX8X5XC+X2R');
INSERT INTO encoding_tests VALUES (47.858925, -75.223290, 3446473125, 858330808, 14, '87V6VQ5G+HMG454');
INSERT INTO encoding_tests VALUES (-17.150, -84.306, 1821250000, 783925248, 12, '56JQVM2V+2J22');
INSERT INTO encoding_tests VALUES (-95.31345221, -172.90260796, 0, 58141835, 15, '2229232W+2X24245');
INSERT INTO encoding_tests VALUES (-79.859625, 177.096808, 253509375, 2925337051, 14, '2VGV43RW+5P3534');
INSERT INTO encoding_tests VALUES (88.265429, -198.447568, 4456635725, 2797997522, 14, 'CVW37H82+5XF5V2');
INSERT INTO encoding_tests VALUES (13.325, 34.920, 2583125000, 1760624640, 2, '7G000000+');
INSERT INTO encoding_tests VALUES (-63.6, -145.4, 660000000, 283443200, 2, '33000000+');
INSERT INTO encoding_tests VALUES (-54.4872370910, -142.4976735090, 887819072, 307219058, 15, '33QVGG72+4W4FHRG');
INSERT INTO encoding_tests VALUES (89.796622, 61.685912, 4494915550, 1979890991, 6, 'CJX3QM00+');
INSERT INTO encoding_tests VALUES (-25.2, 50.7, 1620000000, 1889894400, 8, '5H6GRP22+');
INSERT INTO encoding_tests VALUES (-78.7376, 66.6281, 281560000, 2020377395, 6, '2JH87J00+');
INSERT INTO encoding_tests VALUES (-83.5768747454, -84.1155546149, 160578131, 785485376, 10, '268QCVFM+7Q');
INSERT INTO encoding_tests VALUES (87.1741743283, -98.9097172279, 4429354358, 664291596, 10, 'C6V353FR+M4');
INSERT INTO encoding_tests VALUES (-92.1234, 147.2214, 0, 2680597708, 6, '2R292600+');
INSERT INTO encoding_tests VALUES (-96.081, 30.930, 0, 1727938560, 14, '2G2G2W2J+222222');
INSERT INTO encoding_tests VALUES (58.544790, 0.954987, 3713619750, 1482383253, 4, '9FC20000+');
INSERT INTO encoding_tests VALUES (85.223791, 166.317567, 4380594775, 2837033508, 8, 'CVQ868F9+');
INSERT INTO encoding_tests VALUES (22.4144501873, 161.5737330425, 2810361254, 2798172021, 15, '7VJ3CH7F+QFQ353V');
INSERT INTO encoding_tests VALUES (-81, -189, 225000000, 2875392000, 4, '2VFH0000+');
INSERT INTO encoding_tests VALUES (-3.87, 106.31, 2153250000, 2345451520, 6, '6P884800+');
INSERT INTO encoding_tests VALUES (-86.07687005, 17.43081941, 98078248, 1617353272, 14, '2F5VWCFJ+7842XW');
INSERT INTO encoding_tests VALUES (4.00247742, -147.71777983, 2350061935, 264455947, 6, '63PJ2700+');
INSERT INTO encoding_tests VALUES (-34.13283986879, 143.93778642288, 1396679003, 2653698346, 2, '4R000000+');
INSERT INTO encoding_tests VALUES (-42.77927502, 197.58056291, 1180518124, 144019971, 13, '429V6HCJ+76PRR');
INSERT INTO encoding_tests VALUES (71.797168141, 116.102605255, 4044929203, 2425672542, 15, 'CP3RQ4W3+V29MM5P');
INSERT INTO encoding_tests VALUES (-14.52796652, -19.29446968, 1886800837, 1316499704, 13, '5CQ2FPC4+R669Q');
INSERT INTO encoding_tests VALUES (-46.42436011120, -134.97185393078, 1089390997, 368870572, 11, '4457H2GH+772');
INSERT INTO encoding_tests VALUES (-83.95, 57.33, 151250000, 1944207360, 12, '2H8V382J+2222');
INSERT INTO encoding_tests VALUES (-81.15680196, 116.13215255, 221079951, 2425914593, 12, '2PCRR4VJ+7VCX');
INSERT INTO encoding_tests VALUES (-69.8553608, 38.5416297, 503615980, 1790293030, 10, '3G2W4GVR+VM');
INSERT INTO encoding_tests VALUES (70.06392017, 142.68513577, 4001598004, 2643436632, 8, 'CR243M7P+');
INSERT INTO encoding_tests VALUES (-37.87035641911, 31.45160895416, 1303241089, 1732211580, 15, '4GJH4FH2+VJ5MQHR');
INSERT INTO encoding_tests VALUES (-3.31237547, 55.93515507, 2167190613, 1932780790, 15, '6H8QMWQP+23RXXFP');
INSERT INTO encoding_tests VALUES (-36.7954655, 151.3817689, 1330113362, 2714679450, 14, '4RMH693J+RP68VG');
INSERT INTO encoding_tests VALUES (95.854385181, 79.466306447, 4499999999, 2125547982, 10, 'CJXXXFX8+XG');
INSERT INTO encoding_tests VALUES (31.53982775, 98.72663309, 3038495693, 2283328578, 11, '8M3WGPQG+WMJ');
INSERT INTO encoding_tests VALUES (25.5118795897, 57.7948659543, 2887796989, 1948015541, 14, '7HQVGQ6V+QW54XF');
INSERT INTO encoding_tests VALUES (71, 121, 4025000000, 2465792000, 2, 'CQ000000+');
INSERT INTO encoding_tests VALUES (-82, -9, 200000000, 1400832000, 2, '2C000000+');
INSERT INTO encoding_tests VALUES (-76.08163425, 173.15964020, 347959143, 2893083772, 6, '2VMMW500+');
INSERT INTO encoding_tests VALUES (40.53562804190, -79.76323109809, 3263390701, 821139610, 2, '87000000+');
INSERT INTO encoding_tests VALUES (-61.40656, -81.69399, 714836000, 805322833, 6, '36CWH800+');
INSERT INTO encoding_tests VALUES (27.8722, -178.2141, 2946805000, 14630092, 10, '72V3VQCP+V9');
INSERT INTO encoding_tests VALUES (-92.2718492, 40.5508329, 0, 1806752423, 11, '2H222H22+284');
INSERT INTO encoding_tests VALUES (70.3331, -67.4144, 4008327500, 922301235, 15, 'C72J8HMP+66X2525');
INSERT INTO encoding_tests VALUES (-63.163054, 106.207383, 670923650, 2344610881, 6, '3P88R600+');
INSERT INTO encoding_tests VALUES (57.234, 92.971, 3680850000, 2236178432, 15, '9M9J6XMC+JC22222');
INSERT INTO encoding_tests VALUES (37.1, -195.4, 3177500000, 2822963200, 12, '8V964J22+2222');
INSERT INTO encoding_tests VALUES (31.197, 9.919, 3029925000, 1555816448, 8, '8F3F5WW9+');
INSERT INTO encoding_tests VALUES (85.557757154, -182.229592353, 4388943928, 2930855179, 12, 'CVQVHQ5C+4536');
INSERT INTO encoding_tests VALUES (1.50383657, -69.55623429, 2287595914, 904755328, 4, '67HG0000+');
INSERT INTO encoding_tests VALUES (50.409, 7.402, 3510225000, 1535197184, 15, '9F29CC52+JR22222');
INSERT INTO encoding_tests VALUES (-88, 30, 50000000, 1720320000, 11, '2G4G2222+222');
INSERT INTO encoding_tests VALUES (-98, 139, 0, 2613248000, 10, '2Q2X2222+22');
INSERT INTO encoding_tests VALUES (11.4, 150.4, 2535000000, 2706636800, 4, '7R3G0000+');
INSERT INTO encoding_tests VALUES (-88.504244, 67.742247, 37393900, 2029504487, 4, '2J390000+');
INSERT INTO encoding_tests VALUES (-84.13904, -22.90719, 146524000, 1286904299, 8, '297VV36V+');
INSERT INTO encoding_tests VALUES (-12.874997750, -26.081150643, 1928125056, 1260903213, 12, '59VM4WG9+2G52');
INSERT INTO encoding_tests VALUES (-95.978240742, 83.957497847, 0, 2162339822, 15, '2M252X24+2X55454');
INSERT INTO encoding_tests VALUES (52.797623, 55.332651, 3569940575, 1927845076, 2, '9H000000+');
INSERT INTO encoding_tests VALUES (-25.57754103, -60.87933236, 1610561474, 975836509, 15, '576XC4CC+X7M7MXV');
INSERT INTO encoding_tests VALUES (57.1960, 82.5535, 3679900000, 2150838272, 14, '9M945HW3+CC2222');
INSERT INTO encoding_tests VALUES (-26, 27, 1600000000, 1695744000, 8, '5G692222+');
INSERT INTO encoding_tests VALUES (-27.0, -122.3, 1575000000, 472678400, 11, '545V2P22+222');
INSERT INTO encoding_tests VALUES (-99.118211, 34.329996, 0, 1755791327, 8, '2G2P282H+');
INSERT INTO encoding_tests VALUES (25.33671, 8.65920, 2883417750, 1545496166, 8, '7FQC8MP5+');
INSERT INTO encoding_tests VALUES (-77.54, 110.22, 311500000, 2377482240, 11, '2PJGF66C+222');
INSERT INTO encoding_tests VALUES (-55.69363663291, -8.13133426255, 857659084, 1407948109, 8, '3CPH8V49+');
INSERT INTO encoding_tests VALUES (12.0752578562, 90.0309556122, 2551881446, 2212093588, 15, '7M4G32GJ+4948FV6');
INSERT INTO encoding_tests VALUES (-38.11355992107, -14.54083447411, 1297161001, 1355441483, 13, '4CH7VFP5+HMFM2');
INSERT INTO encoding_tests VALUES (-67.52, -133.23, 562000000, 383139840, 13, '3448FQJC+22222');
INSERT INTO encoding_tests VALUES (-41.5789128, -76.9932090, 1210527180, 843831631, 12, '47C5C2C4+CPMF');
INSERT INTO encoding_tests VALUES (63.50396935, 144.75232815, 3837599233, 2660371072, 6, '9RM6GQ00+');
INSERT INTO encoding_tests VALUES (-99.10, -77.98, 0, 835747840, 11, '2724222C+222');
INSERT INTO encoding_tests VALUES (-13.502, 122.955, 1912450000, 2481807360, 13, '5QR4FXX4+62222');
INSERT INTO encoding_tests VALUES (99.595382598, -71.110954356, 4499999999, 892019061, 12, 'C7XCXVXQ+XJVV');
INSERT INTO encoding_tests VALUES (8.68, 180.22, 2467000000, 1802240, 13, '62W2M6JC+22222');
INSERT INTO encoding_tests VALUES (96.0835607732, -29.0019350420, 4499999999, 1236976148, 10, 'C9XGXXXX+X6');
INSERT INTO encoding_tests VALUES (26.4022965, -31.1647767, 2910057412, 1219258149, 11, '79RCCR2P+W39');
INSERT INTO encoding_tests VALUES (80.99, -174.37, 4274750000, 46120960, 4, 'C2G70000+');
INSERT INTO encoding_tests VALUES (68.0, -35.1, 3950000000, 1187020800, 15, '99W62W22+2222222');
INSERT INTO encoding_tests VALUES (82.4789853525, 71.0194066612, 4311974633, 2056350979, 13, 'CJJHF2H9+HQVC2');
INSERT INTO encoding_tests VALUES (-84.78480, 166.71891, 130380000, 2840321310, 4, '2V780000+');
INSERT INTO encoding_tests VALUES (-10.5782, 25.7779, 1985545000, 1685732556, 11, '5GX7CQCH+P5C');
INSERT INTO encoding_tests VALUES (-3.91348310257, -109.55392470032, 2152162922, 577094248, 13, '658G3CPW+JC4M8');
INSERT INTO encoding_tests VALUES (-55.7416641607, 136.4834168428, 856458395, 2592632150, 11, '3QPR7F5M+89M');
INSERT INTO encoding_tests VALUES (-55.80137, 105.59937, 854965750, 2339630039, 2, '3P000000+');
INSERT INTO encoding_tests VALUES (70.49, 104.87, 4012250000, 2333655040, 2, 'CP000000+');
INSERT INTO encoding_tests VALUES (1.6479856942, 181.1761286225, 2291199642, 9634845, 14, '62H3J5XG+5FRC3Q');
INSERT INTO encoding_tests VALUES (-94.2098, 53.1707, 0, 1910134374, 14, '2H2M252C+274343');
INSERT INTO encoding_tests VALUES (96.6461284508, 37.5309875240, 4499999999, 1782013849, 4, 'CGXV0000+');
INSERT INTO encoding_tests VALUES (13.403331980, 132.878412474, 2585083299, 2563099954, 13, '7Q5JCV3H+89M69');
INSERT INTO encoding_tests VALUES (23.01778459, -75.75490333, 2825444614, 853975831, 10, '77M6269W+42');
INSERT INTO encoding_tests VALUES (-48.4381338, 140.8468367, 1039046655, 2628377286, 6, '4R32HR00+');
INSERT INTO encoding_tests VALUES (-38.2448857266, -111.9149619865, 1293877856, 557752631, 10, '45HCQ34P+22');
INSERT INTO encoding_tests VALUES (-64.0, -94.4, 650000000, 701235200, 4, '36870000+');
INSERT INTO encoding_tests VALUES (-47.0346874447, -51.1267770629, 1074132813, 1055729442, 6, '484CXV00+');
INSERT INTO encoding_tests VALUES (66.6814, -78.9160, 3917035000, 828080128, 8, '97R3M3JM+');
INSERT INTO encoding_tests VALUES (-82.22446, 143.24158, 194388500, 2647995023, 4, '2R950000+');
INSERT INTO encoding_tests VALUES (-31.80606, -102.08156, 1454848500, 638307860, 4, '45WV0000+');
INSERT INTO encoding_tests VALUES (14.94989456, 96.10671106, 2623747364, 2261866177, 12, '7M6RW4X4+XM4Q');
INSERT INTO encoding_tests VALUES (-15.10033816850, 99.53259414053, 1872491545, 2289931011, 11, '5MPXVGXM+V29');
INSERT INTO encoding_tests VALUES (-69.4546558690, 97.3697260830, 513633603, 2272212796, 6, '3M2VG900+');
INSERT INTO encoding_tests VALUES (47.6915368, -109.0087879, 3442288420, 581560009, 6, '85VGMX00+');
INSERT INTO encoding_tests VALUES (99.2751473, 147.8120144, 4499999999, 2685436021, 10, 'CRX9XRX6+XR');
INSERT INTO encoding_tests VALUES (27.6309, -98.7061, 2940772500, 665959628, 2, '76000000+');
INSERT INTO encoding_tests VALUES (27.24379, 92.39247, 2931094750, 2231439114, 12, '7MVJ69VR+GX9J');
INSERT INTO encoding_tests VALUES (-79.78071, 133.66290, 255482250, 2569526476, 10, '2QGM6M97+P5');
INSERT INTO encoding_tests VALUES (-94.55098016, -95.68553772, 0, 690704074, 11, '26262827+2Q4');
INSERT INTO encoding_tests VALUES (-18.100, -83.091, 1797500000, 793878528, 13, '56HRWW25+2J222');
INSERT INTO encoding_tests VALUES (-35.015055, 73.717570, 1374623625, 2078454333, 12, '4JPMXPM9+X2GR');
INSERT INTO encoding_tests VALUES (-87.7171, 177.5628, 57072500, 2929154457, 13, '2V4V7HM7+54743');
INSERT INTO encoding_tests VALUES (56.55872, 54.19708, 3663968000, 1918542479, 11, '9H8PH55W+FRP');
INSERT INTO encoding_tests VALUES (-28.6420, 71.2607, 1533950000, 2058327654, 11, '5J3H9756+674');
INSERT INTO encoding_tests VALUES (-44.755, 21.329, 1131125000, 1649287168, 10, '4G7368WH+2J');
INSERT INTO encoding_tests VALUES (-58.284, 44.435, 792900000, 1838571520, 15, '3HH6PC8P+C222222');
INSERT INTO encoding_tests VALUES (13.469, -118.034, 2586725000, 507625472, 11, '7553FX98+JC2');
INSERT INTO encoding_tests VALUES (40.615, 173.901, 3265375000, 2899156992, 2, '8V000000+');
INSERT INTO encoding_tests VALUES (62, -95, 3800000000, 696320000, 12, '96J72222+2222');
INSERT INTO encoding_tests VALUES (-5.2221889, 139.2054401, 2119445277, 2614930965, 4, '6Q6X0000+');
INSERT INTO encoding_tests VALUES (-24.8, -7.1, 1630000000, 1416396800, 4, '5C7J0000+');
INSERT INTO encoding_tests VALUES (41.5, 0.4, 3287500000, 1477836800, 8, '8FH2GC22+');
INSERT INTO encoding_tests VALUES (-58.89638156814, -177.07241353875, 777590460, 23982788, 8, '32H44W3H+');
INSERT INTO encoding_tests VALUES (99.9924124, 168.8859945, 4499999999, 2858074066, 13, 'CVXCXVXP+X9XXV');
INSERT INTO encoding_tests VALUES (-81.83814, 13.38568, 204046500, 1584215490, 10, '2FCM596P+P7');
INSERT INTO encoding_tests VALUES (-81.641294, -26.677758, 208967650, 1256015806, 12, '29CM985C+FVQ8');
INSERT INTO encoding_tests VALUES (-38.1, -34.8, 1297500000, 1189478400, 6, '49H7W600+');
INSERT INTO encoding_tests VALUES (30.760710361, 5.623188694, 3019017759, 1520625161, 12, '8F27QJ6F+77PC');
INSERT INTO encoding_tests VALUES (-41, -7, 1225000000, 1417216000, 8, '4CFM2222+');
INSERT INTO encoding_tests VALUES (80.2976, 17.4494, 4257440000, 1617505484, 6, 'CFGV7C00+');
INSERT INTO encoding_tests VALUES (-0.8932, -141.8127, 2227670000, 312830361, 10, '63FW454P+PW');
INSERT INTO encoding_tests VALUES (51.1973191264, -176.2844505770, 3529932978, 30437780, 14, '92355PW8+W6FPV3');
INSERT INTO encoding_tests VALUES (64.3538, 37.6501, 3858845000, 1782989619, 6, '9GPV9M00+');
INSERT INTO encoding_tests VALUES (-7.741571, -114.569063, 2056460725, 536010235, 4, '65470000+');
INSERT INTO encoding_tests VALUES (59.668, -73.133, 3741700000, 875454464, 6, '97F8MV00+');
INSERT INTO encoding_tests VALUES (72.146589, -166.255204, 4053664725, 112597368, 4, 'C24M0000+');
INSERT INTO encoding_tests VALUES (45.7536561, -77.9826424, 3393841402, 835726193, 11, '87Q4Q238+FW9');
INSERT INTO encoding_tests VALUES (20.59532, 58.43522, 2764883000, 1953261322, 15, '7HGWHCWP+43HR244');
INSERT INTO encoding_tests VALUES (-2.22208790893, -129.52868305886, 2194447802, 413461028, 11, '649GQFHC+5G8');
INSERT INTO encoding_tests VALUES (21.37734168211, -19.82122122854, 2784433542, 1312184555, 2, '7C000000+');
INSERT INTO encoding_tests VALUES (71.0833633113, -21.3584667975, 4027084082, 1299591439, 12, 'C93W3JMR+8JVC');
INSERT INTO encoding_tests VALUES (48.64, 42.02, 3466000000, 1818787840, 10, '8HW4J2RC+22');
INSERT INTO encoding_tests VALUES (2.28, 65.18, 2307000000, 2008514560, 11, '6JJ775JJ+222');
INSERT INTO encoding_tests VALUES (66, -15, 3900000000, 1351680000, 14, '9CR72222+222222');
INSERT INTO encoding_tests VALUES (82.988994321, -114.039676643, 4324724858, 540346968, 2, 'C5000000+');
INSERT INTO encoding_tests VALUES (-32.04, -9.54, 1449000000, 1396408320, 11, '4CVGXF66+222');
INSERT INTO encoding_tests VALUES (98.43557, -184.42545, 4499999999, 2912866713, 12, 'CVXQXHXF+XRVW');
INSERT INTO encoding_tests VALUES (71.75744246, -62.00099498, 4043936061, 966647849, 2, 'C7000000+');
INSERT INTO encoding_tests VALUES (51.089925, 72.339482, 3527248125, 2067165036, 15, '9J3J38QQ+XQH3452');

-- The subselect in the FROM clause calls the functions, the outer SELECT checks the results.
SELECT
  CASE
    WHEN latitude_integer <> latitude_integer_got
    THEN raise_error(format('latitudeToInteger(%I): got %I, want %I', latitude_degrees, latitude_integer_got, latitude_integer))
    ELSE ROW_NUMBER() OVER ()
  END AS latitudeToInteger,
  CASE
    WHEN longitude_integer <> longitude_integer_got
    THEN raise_error(format('longitudeToInteger(%I): got %I, want %I', longitude_degrees, longitude_integer_got, longitude_integer))
    ELSE ROW_NUMBER() OVER ()
  END AS longitudeToInteger,
  CASE
    WHEN code <> code_got
    THEN raise_error(format('encodeIntegers(%I, %I, %I): got %I, want %I', latitude_integer, longitude_integer, code_length, code_got, code))
    ELSE ROW_NUMBER() OVER ()
  END AS encodeIntegers
FROM (
  SELECT
    *,
    pluscode_latitudeToInteger(latitude_degrees) AS latitude_integer_got,
    pluscode_longitudeToInteger(longitude_degrees) longitude_integer_got,
    pluscode_encodeIntegers(latitude_integer, longitude_integer, code_length) AS code_got
  FROM encoding_tests
) AS test_data;
